* Change the directory to the top one for this project
set more off
capture cd "C:\Users\Benjamin Allen\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"
capture cd "C:\Users\balle\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"
capture cd "~\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"
capture cd "C:\Users\balle\Dropbox (Personal)\Papers\Support for Algorithmic Governance"

********************************************************************************
* Load EPIS data
********************************************************************************

use "data/EPIS_data recoded and labeled.dta", clear

********************************************************************************
* Append Canadian data
********************************************************************************

append using "data/Canadian data for combining with EPIS.dta", gen(source)

lab def source 0 "EPIS" 1 "Canada"
lab val source source

********************************************************************************
* Review and recode variables with differring coding
********************************************************************************

*------------------------------------------------------------------------------*
* Country
*------------------------------------------------------------------------------*

tab country_recoded source

*------------------------------------------------------------------------------*
* Algorithm Acceptability
*------------------------------------------------------------------------------*

tab alg_govt_acceptable_1_ord source, col

tab alg_govt_acceptable_1_bi source, col

*------------------------------------------------------------------------------*
* Age
*------------------------------------------------------------------------------*

bys source: sum age_in_years

*------------------------------------------------------------------------------*
* Female
*------------------------------------------------------------------------------*

tab female source, col

*------------------------------------------------------------------------------*
* Income - recode
*------------------------------------------------------------------------------*
 
tab subjective_income source, m col
lab li subjective_income

tab income_cat_canada source, m col
lab li income

* The definition of this variable is kind of awkward, combining a subjective with an objective measure, but it's the best I can come up with
* The Low Income Cut Off (LICO) in 2018 in Canada for a family of 4 was between $26,567 (for Rural communities) and $40,614 for big cities.
* ( https://www150.statcan.gc.ca/t1/tbl1/en/tv.action?pid=1110024101 )
* so using 40k as the breakpoint seems reasonable. 
* It also roughly matches the distribution in the EPIS data, with ~30% of respondents in each dataset then classified as "low income"

gen low_income = .

replace low_income = 1 if subjective_income == 0 // "Finding it very difficult on present income"
replace low_income = 1 if subjective_income == 1 // "Finding it difficult on present income"

replace low_income = 0 if subjective_income == 2 // "Coping on present income"
replace low_income = 0 if subjective_income == 3 // "Living comfortably on present income"

replace low_income = 1 if income_cat_canada == 1 // "Less than $20,000"
replace low_income = 1 if income_cat_canada == 2 // "$20,000 to $39,999"

replace low_income = 0 if income_cat_canada == 3 // "$40,000 to $59,999"
replace low_income = 0 if income_cat_canada == 4 // "$60,000 to $79,999"
replace low_income = 0 if income_cat_canada == 5 // "$80,000 to $99,999"
replace low_income = 0 if income_cat_canada == 6 // "$100,000 to $119,999"
replace low_income = 0 if income_cat_canada == 7 // "$120,000 to $149,999"
replace low_income = 0 if income_cat_canada == 8 // "$150,000 to $199,999"
replace low_income = 0 if income_cat_canada == 9 // "$200,000 or more"

lab var low_income "Low income (0/1)"
lab def low_income 0 "Not low income" 1 "Low income"
lab val low_income low_income

tab low_income country_recoded, m col

*------------------------------------------------------------------------------*
* Education - recode
*------------------------------------------------------------------------------*

tab education_cat source, m

tab education_canada source, m

lab li education_cat education

	* 0 Incomplete Secondary Education
	replace education_cat = 0 if education_canada == 1 // Some high school

	* 1 Secondary Education Completed
	replace education_cat = 1 if education_canada == 2 // High school diploma
	replace education_cat = 1 if education_canada == 4 // Some university - this is the biggest change, as they haven't yet completed it

	* 2 Vocational or Professional Certification Completed
	replace education_cat = 2 if education_canada == 3 // College or technical degree

	* 3 University Education Completed or higher
	replace education_cat = 3 if education_canada == 5 // Bachelor’s degree
	replace education_cat = 3 if education_canada == 6 // Master’s degree
	replace education_cat = 3 if education_canada == 7 // Professional degree
	replace education_cat = 3 if education_canada == 8 // Doctorate

	* . Don't know
	replace education_cat = . if education_canada == 9 // Don't know


* Review
tab education_cat source, m

tab education_cat country_recoded, m

*------------------------------------------------------------------------------*
* L/R scale
*------------------------------------------------------------------------------*

bys source: sum lr_scale

*------------------------------------------------------------------------------*
* Populism items
*------------------------------------------------------------------------------*

tab populism_4 source, m col
tab populism_2 source, m col
tab populism_7 source, m col

********************************************************************************
* Graphing algorithmic governance acceptibility
********************************************************************************

*------------------------------------------------------------------------------*
* All EPIS data together
*------------------------------------------------------------------------------*

gen alg_govt_acceptable_1_bi_pct = alg_govt_acceptable_1_bi * 100
gen alg_govt_acceptable_2_bi_pct = alg_govt_acceptable_2_bi * 100
gen alg_govt_acceptable_3_bi_pct = alg_govt_acceptable_3_bi * 100
gen alg_govt_acceptable_4_bi_pct = alg_govt_acceptable_4_bi * 100
gen alg_govt_acceptable_5_bi_pct = alg_govt_acceptable_5_bi * 100
gen alg_govt_acceptable_6_bi_pct = alg_govt_acceptable_6_bi * 100
gen alg_govt_acceptable_7_bi_pct = alg_govt_acceptable_7_bi * 100
gen alg_govt_acceptable_8_bi_pct = alg_govt_acceptable_8_bi * 100

sum alg_govt_acceptable_1_bi_pct ///
	alg_govt_acceptable_2_bi_pct ///
	alg_govt_acceptable_3_bi_pct ///
	alg_govt_acceptable_4_bi_pct ///
	alg_govt_acceptable_5_bi_pct ///
	alg_govt_acceptable_6_bi_pct ///
	alg_govt_acceptable_7_bi_pct ///
	alg_govt_acceptable_8_bi_pct
	
count
local sample_count = r(N)

* Categories - normal colours
graph bar (mean) ///
	alg_govt_acceptable_1_bi_pct ///
	alg_govt_acceptable_2_bi_pct ///
	alg_govt_acceptable_3_bi_pct ///
	alg_govt_acceptable_4_bi_pct ///
	alg_govt_acceptable_5_bi_pct ///
	alg_govt_acceptable_6_bi_pct ///
	alg_govt_acceptable_7_bi_pct ///
	alg_govt_acceptable_8_bi_pct ///
	, ///
	///percentages ///
	title("How acceptable are the following reasons for governments to""use algorithms and AI to make decisions?", size(medsmall))  ///
	subtitle(`"Percent selected "Acceptable reason to use an algorithm""' "Combined EPIS and Canadian data", size(small)) ///
	ytitle("Percent") ///
	yline(50) ///
	bar(1, color(eltblue*0.7)) bar(2, color(eltblue)) bar(3, color(ebblue*0.7)) bar(4, color(ebblue)) bar(5, color(edkblue*0.7)) bar(6, color(edkblue)) bar(7, color(eltgreen)) bar(8, color(teal)) ///
	ylabel(0(10)100, angle(0) labsize(vsmall))  ///
	legend(order( /// I've put these even-then-odd, to make the result easier to read as two columns
		1 "To reduce the time required to make decisions" ///
		5 "To reduce fraud against the government." ///
		2 "To make decisions which will be a better use of""government money." ///
		6 `"To make decisions which are more consistent"'`"and less “random”."' ///
		3 "To make sure decisions are not influenced by""factors like a program recipient's gender,""ethnicity, or wealth." ///
		7 "To reduce the number of bureaucrats/government""officials." ///
		4 "To make sure decisions are not influenced by""officials’ biases." ///
		8 "To reduce the costs of government." ///
		) size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)  ///
	note("N = `sample_count'", size(vsmall))  ///
	blabel(bar, format(%4.1f) size(medsmall))
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
graph export "analysis/graphs/combined/acceptance for each reason/acceptance for each reason combined data.png", width(2000) replace


* Categories - greyscale
graph bar (mean) ///
	alg_govt_acceptable_1_bi_pct ///
	alg_govt_acceptable_2_bi_pct ///
	alg_govt_acceptable_3_bi_pct ///
	alg_govt_acceptable_4_bi_pct ///
	alg_govt_acceptable_5_bi_pct ///
	alg_govt_acceptable_6_bi_pct ///
	alg_govt_acceptable_7_bi_pct ///
	alg_govt_acceptable_8_bi_pct ///
	, ///
	///percentages ///
	title("How acceptable are the following reasons for governments to""use algorithms and AI to make decisions?", size(medsmall))  ///
	subtitle(`"Percent selected "Acceptable reason to use an algorithm""' "Combined EPIS and Canadian data", size(small)) ///
	ytitle("Percent") ///
	yline(50, lcolor(gs0)) ///
	bar(1, color(gs4)) bar(2, color(gs8)) bar(3, color(gs12)) bar(4, color(gs3)) bar(5, color(gs7)) bar(6, color(gs11)) bar(7, color(gs5)) bar(8, color(gs9)) ///
	ylabel(0(10)100, angle(0) labsize(vsmall))  ///
	legend(order( /// I've put these even-then-odd, to make the result easier to read as two columns
		1 "To reduce the time required to make decisions" ///
		5 "To reduce fraud against the government." ///
		2 "To make decisions which will be a better use of""government money." ///
		6 `"To make decisions which are more consistent"'`"and less “random”."' ///
		3 "To make sure decisions are not influenced by""factors like a program recipient's gender,""ethnicity, or wealth." ///
		7 "To reduce the number of bureaucrats/government""officials." ///
		4 "To make sure decisions are not influenced by""officials’ biases." ///
		8 "To reduce the costs of government." ///
		) size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)  ///
	note("N = `sample_count'", size(vsmall))  ///
	blabel(bar, format(%4.1f) size(medsmall))
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
graph export "analysis/graphs/combined/acceptance for each reason/acceptance for each reason combined data greyscale.png", width(2000) replace


*------------------------------------------------------------------------------*
* For each country
*------------------------------------------------------------------------------*

foreach country_recoded in 	"Austria" "Belgium (FR)" "Belgium (NL)" "Canada" "Denmark" "Finland" "France" "Germany" "Greece" "Ireland" "Italy" "Netherlands" "Norway" "Portugal" "Spain" "Sweden" "UK" {

	preserve

	keep if country_recoded == "`country_recoded'":country_recoded
	
	count
	local sample_count = r(N)

	* Categories 
	graph bar (mean) ///
		alg_govt_acceptable_1_bi_pct ///
		alg_govt_acceptable_2_bi_pct ///
		alg_govt_acceptable_3_bi_pct ///
		alg_govt_acceptable_4_bi_pct ///
		alg_govt_acceptable_5_bi_pct ///
		alg_govt_acceptable_6_bi_pct ///
		alg_govt_acceptable_7_bi_pct ///
		alg_govt_acceptable_8_bi_pct ///
		, ///
		///percentages ///
		title("How acceptable are the following reasons for governments to""use algorithms and AI to make decisions?", size(medsmall))  ///
		subtitle(`"Percent selected "Acceptable reason to use an algorithm""' "`country_recoded'", size(small)) ///
		ytitle("Percent") ///
		yline(50) ///
		bar(1, color(eltblue*0.7)) bar(2, color(eltblue)) bar(3, color(ebblue*0.7)) bar(4, color(ebblue)) bar(5, color(edkblue*0.7)) bar(6, color(edkblue)) bar(7, color(eltgreen)) bar(8, color(teal)) ///
		ylabel(0(10)100, angle(0) labsize(vsmall))  ///
		legend(order( /// I've put these even-then-odd, to make the result easier to read as two columns
			1 "To reduce the time required to make decisions" ///
			5 "To reduce fraud against the government." ///
			2 "To make decisions which will be a better use of""government money." ///
			6 `"To make decisions which are more consistent"'`"and less “random”."' ///
			3 "To make sure decisions are not influenced by""factors like a program recipient's gender,""ethnicity, or wealth." ///
			7 "To reduce the number of bureaucrats/government""officials." ///
			4 "To make sure decisions are not influenced by""officials’ biases." ///
			8 "To reduce the costs of government." ///
			) size(vsmall)) ///
		graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)  ///
		note("N = `sample_count'", size(vsmall))  ///
		blabel(bar, format(%4.1f) size(medsmall))
	local nb=`.Graph.plotregion1.barlabels.arrnels'
	forval i=1/`nb' {
	  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
	  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
	}
	.Graph.drawgraph
	graph export "analysis/graphs/combined/acceptance for each reason/acceptance for each reason combined `country_recoded'.png", width(2000) replace

	restore
}

********************************************************************************
* Algorithmic governance acceptibility scale
********************************************************************************

*--------------------------------------------------------*
* Check scale features - binary version
*--------------------------------------------------------*

 *First, check scale features
factor alg_govt_acceptable_1_bi alg_govt_acceptable_2_bi alg_govt_acceptable_3_bi alg_govt_acceptable_4_bi alg_govt_acceptable_5_bi alg_govt_acceptable_6_bi alg_govt_acceptable_7_bi alg_govt_acceptable_8_bi, pcf factors(4)  mineigen(0)

* Chronbach's Alphas

* Full scale
alpha alg_govt_acceptable_1_bi alg_govt_acceptable_2_bi alg_govt_acceptable_3_bi alg_govt_acceptable_4_bi alg_govt_acceptable_5_bi alg_govt_acceptable_6_bi alg_govt_acceptable_7_bi alg_govt_acceptable_8_bi

*sub-scales
* Efficiency
alpha alg_govt_acceptable_1_bi alg_govt_acceptable_2_bi alg_govt_acceptable_5_bi alg_govt_acceptable_7_bi alg_govt_acceptable_8_bi

* Fairness
alpha alg_govt_acceptable_3_bi alg_govt_acceptable_4_bi alg_govt_acceptable_6_bi

*--------------------------------------------------------*
* Check scale features - ordinal version
*--------------------------------------------------------*

 *First, check scale features
factor alg_govt_acceptable_1_ord alg_govt_acceptable_2_ord alg_govt_acceptable_3_ord alg_govt_acceptable_4_ord alg_govt_acceptable_5_ord alg_govt_acceptable_6_ord alg_govt_acceptable_7_ord alg_govt_acceptable_8_ord, pcf factors(4)  mineigen(0)

* Chronbach's Alpha
alpha alg_govt_acceptable_1_ord alg_govt_acceptable_2_ord alg_govt_acceptable_3_ord alg_govt_acceptable_4_ord alg_govt_acceptable_5_ord alg_govt_acceptable_6_ord alg_govt_acceptable_7_ord alg_govt_acceptable_8_ord

*sub-scales
* Efficiency
alpha alg_govt_acceptable_1_ord alg_govt_acceptable_2_ord alg_govt_acceptable_5_ord alg_govt_acceptable_7_ord alg_govt_acceptable_8_ord

* Fairness
alpha alg_govt_acceptable_3_ord alg_govt_acceptable_4_ord alg_govt_acceptable_6_ord



*------------------------------------------------------------------------------*
* Create scales - ordinal version
*------------------------------------------------------------------------------*

egen alg_govt_acceptable_scale_ord = rowtotal(alg_govt_acceptable_1_ord alg_govt_acceptable_2_ord alg_govt_acceptable_3_ord alg_govt_acceptable_4_ord alg_govt_acceptable_5_ord alg_govt_acceptable_6_ord alg_govt_acceptable_7_ord alg_govt_acceptable_8_ord)

tab alg_govt_acceptable_scale_ord

count
local sample_count = r(N)

graph twoway (hist alg_govt_acceptable_scale_ord , percent bin(20) fcolor(ebblue*0.5) lcolor(ebblue*0.6)) ///
	, title("Acceptance of algorithmic governance", size(medsmall)) ///
	subtitle("Summed score across 8 reasons""Combined EPIS and Canadian data", size(small)) ///
	xtitle("Exposure score", size(small))  ///
	xlabel(-8(1)8, labsize(small)) ///
	ylabel(0(5)30, labsize(medsmall) angle(0)) ///
	note( ///
	"Summed score:" ///
	"-1 = Reason is unacceptable" ///
	"0 = Not sure" ///
	"1 = Reason is acceptable" ///
	"N = `sample_count'" ///
	, size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
graph export "analysis/graphs/combined/number of reasons accepted/hist of alg_govt_acceptable_scale_ord combined data.png", width(3000) replace

count
local sample_count = r(N)

graph bar (percent) , over(alg_govt_acceptable_scale_ord) ///
	title("Acceptance of algorithmic governance", size(medium)) ///
	subtitle("Summed score across 8 reasons""Combined EPIS and Canadian data", size(small)) ///
	ytitle("Percent", size(small)) ///
	ylab(0(10)40, angle(0) labsize(small)) ///
	note( ///
	"Summed score:" ///
	"-1 = Reason is unacceptable" ///
	"0 = Not sure" ///
	"1 = Reason is acceptable" ///
	"N = `sample_count'" ///
	, size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) ///
	bar(1, lcolor(ebblue*0.9) fcolor(eltblue%80)) ///
	blabel(bar, format(%4.1f) size(small))	
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
graph export "analysis/graphs/combined/number of reasons accepted/bargraph of alg_govt_acceptable_scale_ord combined data.png", width(2000) replace


*------------------------------------------------------------------------------*
* Graphing distributions of the number of reasons found acceptable
*------------------------------------------------------------------------------*

*-------------------------------------------------------------*
* All countries overlapped
*-------------------------------------------------------------*

preserve

collapse (count) N = age_in_years, by(alg_govt_acceptable_scale_ord country_recoded)

* Calculating the N for each country
gen country_N = 0
sort country_recoded
by country_recoded: ereplace country_N = total(N)

* Calculate the proportion of the population within each country that supports each number of reasons
gen ord_scale_percent = N / country_N

* Need to re-sort it so that the lines connect in a reasonable way
sort alg_govt_acceptable_scale_ord

graph twoway ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "Austria":country_recoded,		color(red%40)		msymbol(triangle)	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "Belgium (FR)":country_recoded, 	color(sand%40)		msymbol(square) 	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "Belgium (NL)":country_recoded, 	color(sandb%40)		msymbol(diamond) 	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "Canada":country_recoded, 		color(red*1.3%40)	msymbol(square) 	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "Denmark":country_recoded, 		color(cranberry%40)	msymbol(triangle) 	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "Finland":country_recoded, 		color(edkblue%40)	msymbol(square) 	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "France":country_recoded, 		color(ebblue%40)	msymbol(diamond) 	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "Germany":country_recoded, 		color(maroon%40)	msymbol(square) 	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "Greece":country_recoded, 		color(eltblue%40)	msymbol(square) 	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "Ireland":country_recoded, 		color(dkorange%40)	msymbol(triangle) 	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "Italy":country_recoded, 			color(midgreen%40)	msymbol(diamond) 	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "Netherlands":country_recoded, 	color(erose%40)		msymbol(square) 	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "Norway":country_recoded, 		color(pink%40)		msymbol(diamond) 	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "Portugal":country_recoded, 		color(midgreen%40)	msymbol(triangle) 	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "Spain":country_recoded, 			color(gold%40)		msymbol(square) 	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "Sweden":country_recoded, 		color(teal%40)		msymbol(diamond) 	msize(vsmall) ) ///
	(connected ord_scale_percent alg_govt_acceptable_scale_ord if country_recoded == "UK":country_recoded, 			color(navy%40)		msymbol(triangle) 	msize(vsmall) ) ///
	, ///
	title("Comparing distribution of acceptance across countries", size(medsmall)) 	///
	ytitle("Proportion") ///
	ylabels(0(0.1)0.3, angle(0) labsize(small)) ///
	xtitle("Summed score across 8 reasons", size(small)) ///
	xlabels(-8(1)8, labsize(vsmall)) ///
	legend(order( ///
		1 "Austria" ///
		2 "Belgium (FR)" ///
		3 "Belgium (NL)" ///
		4 "Canada" ///
		5 "Denmark" ///
		6 "Finland" ///
		7 "France" ///
		8 "Germany" ///
		9 "Greece" ///
		10 "Ireland" ///
		11 "Italy" ///
		12 "Netherlands" ///
		13 "Norway" ///
		14 "Portugal" ///
		15 "Spain" ///
		16 "Sweden" ///
		17 "UK" ///
		) col(1) pos(3) size(vsmall)) ///
	note( ///
		"Summed score:" ///
		"-1 = Reason is unacceptable" ///
		"0 = Not sure" ///
		"1 = Reason is acceptable" ///
		, size(tiny)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
	
graph export "analysis\graphs\combined\number of reasons accepted\comparing ord_scale distribution across countries.png", width(3300) height(2000) replace

restore

* Rescaling to 0-1
sum alg_govt_acceptable_scale_ord
replace alg_govt_acceptable_scale_ord = (alg_govt_acceptable_scale_ord -r(min)) / (r(max)-r(min))
sum alg_govt_acceptable_scale_ord
lab var alg_govt_acceptable_scale_ord "Ordered acceptance of algorithmic governance (scaled 0-1)"

*---------------------------------------*
* Sub-scales
*---------------------------------------*

* Efficiency
egen alg_govt_acc_eff_scale_ord = rowtotal( alg_govt_acceptable_1_ord alg_govt_acceptable_2_ord alg_govt_acceptable_5_ord alg_govt_acceptable_7_ord alg_govt_acceptable_8_ord)

* Fairness
egen alg_govt_acc_fair_scale_ord = rowtotal( alg_govt_acceptable_3_ord alg_govt_acceptable_4_ord alg_govt_acceptable_6_ord) 

count
local sample_count = r(N)

graph twoway ///
	(scatter alg_govt_acc_fair_scale_ord alg_govt_acc_eff_scale_ord , jitter(6) mcolor(ebblue%10) msize(tiny)) ///
	(lfit alg_govt_acc_fair_scale_ord alg_govt_acc_eff_scale_ord ) ///
	, ///
	title("Average score of reasons", size(medsmall)) ///
	subtitle("Comparing dimensions""Combined EPIS and Canadian data", size(small)) ///
	ytitle("Fairness") ///
	xtitle("Efficiency") ///
	ylabel(-3(1)3) ///
	xlabel(-5(1)5) ///
	legend(off) ///
	note("N = `sample_count'", size(vsmall))  ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
graph export "analysis/graphs/combined/scatterplots/scatterplot of alg_govt_acc_fair_scale_ord and alg_govt_acc_eff_scale_ord combined data.png", width(3000) replace

* Rescaling to 0-1

* Efficiency
sum alg_govt_acc_eff_scale_ord
replace alg_govt_acc_eff_scale_ord = (alg_govt_acc_eff_scale_ord -r(min)) / (r(max)-r(min))
sum alg_govt_acc_eff_scale_ord
lab var alg_govt_acc_eff_scale_ord "Ordered acceptance of algorithmic governance efficiency items (scaled 0-1)"

* Fairness
sum alg_govt_acc_fair_scale_ord
replace alg_govt_acc_fair_scale_ord = (alg_govt_acc_fair_scale_ord -r(min)) / (r(max)-r(min))
sum alg_govt_acc_fair_scale_ord
lab var alg_govt_acc_fair_scale_ord "Ordered acceptance of algorithmic governance fairness items (scaled 0-1)"

*------------------------------------------------------------------------------*
* Create scales - binary version
*------------------------------------------------------------------------------*

egen alg_govt_acceptable_scale_bi = rowtotal(alg_govt_acceptable_1_bi alg_govt_acceptable_2_bi alg_govt_acceptable_3_bi alg_govt_acceptable_4_bi alg_govt_acceptable_5_bi alg_govt_acceptable_6_bi alg_govt_acceptable_7_bi alg_govt_acceptable_8_bi)

tab alg_govt_acceptable_scale_bi

bys country_recoded: tab alg_govt_acceptable_scale_bi

gen support_majority_reasons = 0
replace support_majority_reasons = 1 if alg_govt_acceptable_scale_bi >= 5

tab country_recoded support_majority_reasons, row

sum alg_govt_acceptable_scale_bi

bys country_recoded: sum alg_govt_acceptable_scale_bi


/* OLD WAY OF GRAPHING IT - the bars don't look quite right this way, and we can't label them

* Graphing policy activism on its own
graph twoway (hist alg_govt_acceptable_scale_bi , percent  fcolor(ebblue*0.5) lcolor(ebblue*0.6)) ///
	, title("Acceptance of algorithmic governance", size(medsmall)) ///
	subtitle("Total accepted among 8 reasons", size(small)) ///
	xtitle("Number of reasons found acceptable", size(small))  ///
	xlabel(0(1)8, labsize(small)) ///
	ylabel(0(5)30, labsize(medsmall) angle(0)) ///
	note( ///
	"Average score of reasons:" ///
	"0 = Unacceptable or Not sure" ///
	"1 = Reason is acceptable" ///
	, size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
graph export "analysis/graphs/number of reasons accepted/hist of alg_govt_acceptable_scale_bi.png", width(3000) replace

*/

count
local sample_count = r(N)

* Colour version
graph bar (percent) , over(alg_govt_acceptable_scale_bi) ///
	title("Acceptance of algorithmic governance", size(medium)) ///
	subtitle("Number of reasons found acceptable""Combined EPIS and Canadian data", size(small)) ///
	ytitle("Percent", size(small)) ///
	ylab(0(10)40, angle(0) labsize(small)) ///
	///note("Weighted by province, age group, and gender", size(vsmall))  ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) ///
	bar(1, lcolor(ebblue*0.9) fcolor(eltblue%80)) ///
	note("N = `sample_count'", size(vsmall))  ///
	blabel(bar, format(%4.1f) size(medsmall))	
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
graph export "analysis/graphs/combined/number of reasons accepted/bargraph of alg_govt_acceptable_scale_bi combined data.png", width(2000) replace


* Greyscale for publication
graph bar (percent) , over(alg_govt_acceptable_scale_bi) ///
	title("Acceptance of algorithmic governance", size(medium)) ///
	subtitle("Number of reasons found acceptable""Combined EPIS and Canadian data", size(small)) ///
	ytitle("Percent", size(small)) ///
	ylab(0(10)40, angle(0) labsize(small)) ///
	///note("Weighted by province, age group, and gender", size(vsmall))  ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) ///
	bar(1, lcolor(gs6) fcolor(gs9)) ///
	note("N = `sample_count'", size(vsmall))  ///
	blabel(bar, format(%4.1f) size(medsmall))	
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
graph export "analysis/graphs/combined/number of reasons accepted/bargraph of alg_govt_acceptable_scale_bi combined data greyscale.png", width(2000) replace


*------------------------------------------------------------------------------*
* Comparing distributions of the number of reasons found acceptable in different countries
*------------------------------------------------------------------------------*

*--------------------------------------------------------------*
* Overlapped connected line graph, to show similarities
*--------------------------------------------------------------*

preserve

collapse (count) N = age_in_years, by(alg_govt_acceptable_scale_bi country_recoded)

* Calculating the N for each country
gen country_N = 0
sort country_recoded
by country_recoded: ereplace country_N = total(N)

* Calculate the proportion of the population within each country that supports each number of reasons
gen num_reasons_percent = N / country_N

* Need to re-sort it so that the lines connect in a reasonable way
sort alg_govt_acceptable_scale_bi

graph twoway ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "Austria":country_recoded,		color(red%40)		msymbol(triangle)	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "Belgium (FR)":country_recoded, 	color(sand%40)		msymbol(square) 	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "Belgium (NL)":country_recoded, 	color(sandb%40)		msymbol(diamond) 	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "Canada":country_recoded, 		color(red*1.3%40)	msymbol(square) 	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "Denmark":country_recoded, 		color(cranberry%40)	msymbol(triangle) 	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "Finland":country_recoded, 		color(edkblue%40)	msymbol(square) 	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "France":country_recoded, 		color(ebblue%40)	msymbol(diamond) 	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "Germany":country_recoded, 		color(maroon%40)	msymbol(square) 	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "Greece":country_recoded, 		color(eltblue%40)	msymbol(square) 	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "Ireland":country_recoded, 		color(dkorange%40)	msymbol(triangle) 	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "Italy":country_recoded, 			color(midgreen%40)	msymbol(diamond) 	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "Netherlands":country_recoded, 	color(erose%40)		msymbol(square) 	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "Norway":country_recoded, 		color(pink%40)		msymbol(diamond) 	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "Portugal":country_recoded, 		color(midgreen%40)	msymbol(triangle) 	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "Spain":country_recoded, 			color(gold%40)		msymbol(square) 	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "Sweden":country_recoded, 		color(teal%40)		msymbol(diamond) 	msize(vsmall) ) ///
	(connected num_reasons_percent alg_govt_acceptable_scale_bi if country_recoded == "UK":country_recoded, 			color(navy%40)		msymbol(triangle) 	msize(vsmall) ) ///
	, ///
	title("Comparing distribution of acceptance across countries", size(medsmall)) 	///
	ytitle("Proportion") ///
	ylabels(0(0.1)0.4, angle(0) labsize(small)) ///
	xtitle("Number of reasons found acceptable", size(small)) ///
	xlabels(0(1)8, labsize(vsmall)) ///
	legend(order( ///
		1 "Austria" ///
		2 "Belgium (FR)" ///
		3 "Belgium (NL)" ///
		4 "Canada" ///
		5 "Denmark" ///
		6 "Finland" ///
		7 "France" ///
		8 "Germany" ///
		9 "Greece" ///
		10 "Ireland" ///
		11 "Italy" ///
		12 "Netherlands" ///
		13 "Norway" ///
		14 "Portugal" ///
		15 "Spain" ///
		16 "Sweden" ///
		17 "UK" ///
		) col(1) pos(3) size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
	
graph export "analysis\graphs\combined\number of reasons accepted\comparing num_reasons distribution across countries.png", width(3300) height(2000) replace

restore

*--------------------------------------------------------------*
* Each country broken out
*--------------------------------------------------------------*


foreach country_recoded in 	"Austria" "Belgium (FR)" "Belgium (NL)" "Canada" "Denmark" "Finland" "France" "Germany" "Greece" "Ireland" "Italy" "Netherlands" "Norway" "Portugal" "Spain" "Sweden" "UK" {

	preserve

	keep if country_recoded == "`country_recoded'":country_recoded
	
	count
	local sample_count = r(N)

	
	* Normal colour version
	graph bar (percent) , over(alg_govt_acceptable_scale_bi) ///
		title("`country_recoded'", size(medium)) ///
		///subtitle("Number of reasons found acceptable", size(small)) ///
		ytitle("Percent", size(small)) ///
		ylab(0(10)30, gmin gmax angle(0) labsize(small)) ///
		///note("Weighted by province, age group, and gender", size(vsmall))  ///
		graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) ///
		bar(1, lcolor(ebblue*0.9) fcolor(eltblue%80)) ///
		note("N = `sample_count'", size(vsmall))  ///
		blabel(bar, format(%4.1f) size(vsmall))	
	local nb=`.Graph.plotregion1.barlabels.arrnels'
	forval i=1/`nb' {
	  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
	  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
	}
	.Graph.drawgraph
	graph save "analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi `country_recoded'.gph", replace

	* Greyscale version for publication
	graph bar (percent) , over(alg_govt_acceptable_scale_bi) ///
		title("`country_recoded'", size(medium)) ///
		///subtitle("Number of reasons found acceptable", size(small)) ///
		ytitle("Percent", size(small)) ///
		ylab(0(10)30, gmin gmax angle(0) labsize(small)) ///
		///note("Weighted by province, age group, and gender", size(vsmall))  ///
		graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) ///
		bar(1, lcolor(gs8) fcolor(gs11)) ///
		note("N = `sample_count'", size(vsmall))  ///
		blabel(bar, format(%4.1f) size(vsmall))	
	local nb=`.Graph.plotregion1.barlabels.arrnels'
	forval i=1/`nb' {
	  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
	  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
	}
	.Graph.drawgraph
	graph save "analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi `country_recoded' greyscale.gph", replace
	
	restore
}


*--------------------------------------------------------------*
* Pooled countries, in the same format
*--------------------------------------------------------------*

count
local sample_count = r(N)

* Normal colour version
graph bar (percent) , over(alg_govt_acceptable_scale_bi) ///
	title("All countries pooled", size(medium)) ///
	///subtitle("Number of reasons found acceptable", size(small)) ///
	ytitle("Percent", size(small)) ///
	ylab(0(10)30, gmin gmax angle(0) labsize(small)) ///
	///note("Weighted by province, age group, and gender", size(vsmall))  ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) ///
	bar(1, lcolor(edkblue*0.9) fcolor(ebblue%80)) ///
	note("N = `sample_count'", size(vsmall))  ///
	blabel(bar, format(%4.1f) size(vsmall))	
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
graph save "analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi all countries pooled.gph", replace


* Greyscale version for publication
graph bar (percent) , over(alg_govt_acceptable_scale_bi) ///
	title("All countries pooled", size(medium)) ///
	///subtitle("Number of reasons found acceptable", size(small)) ///
	ytitle("Percent", size(small)) ///
	ylab(0(10)30, gmin gmax angle(0) labsize(small)) ///
	///note("Weighted by province, age group, and gender", size(vsmall))  ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) ///
	bar(1, lcolor(gs4) fcolor(gs7)) ///
	note("N = `sample_count'", size(vsmall))  ///
	blabel(bar, format(%4.1f) size(vsmall))	
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
graph save "analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi all countries pooled greyscale.gph", replace

*--------------------------------------------------------------*
* Combining into two pages
*--------------------------------------------------------------*

*----------------------------------------------------*
* Normal colour graphs
*----------------------------------------------------*

*--------------------------------*
* First 9: All-Germany
*--------------------------------*

graph combine ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi all countries pooled.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Austria.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Belgium (FR).gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Belgium (NL).gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Canada.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Denmark.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Finland.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi France.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Germany.gph" ///
	, ///
	title("Acceptance of algorithmic governance", size(medium)) ///
	subtitle("Number of reasons found acceptable by country", size(small)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
/*
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
*/
graph export "analysis/graphs/combined/number of reasons accepted/combined bargraphs of alg_govt_acceptable_scale_bi All-Germany.png", width(2000) replace
	

*--------------------------------*
* Second 9: Greece-UK
*--------------------------------*	

graph combine ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Greece.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Ireland.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Italy.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Netherlands.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Norway.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Portugal.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Spain.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Sweden.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi UK.gph" ///
	, ///
	title("Acceptance of algorithmic governance", size(medium)) ///
	subtitle("Number of reasons found acceptable by country", size(small)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
/*
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
*/
graph export "analysis/graphs/combined/number of reasons accepted/combined bargraphs of alg_govt_acceptable_scale_bi Greece-UK.png", width(2000) replace

*----------------------------------------------------*
* Greyscale for publication
*----------------------------------------------------*

*--------------------------------*
* First 9: All-Germany
*--------------------------------*

graph combine ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi all countries pooled greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Austria greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Belgium (FR) greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Belgium (NL) greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Canada greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Denmark greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Finland greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi France greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Germany greyscale.gph" ///
	, ///
	title("Acceptance of algorithmic governance", size(medium)) ///
	subtitle("Number of reasons found acceptable by country", size(small)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
/*
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
*/
graph export "analysis/graphs/combined/number of reasons accepted/combined bargraphs of alg_govt_acceptable_scale_bi All-Germany greyscale.png", width(2000) replace
	

*--------------------------------*
* Second 9: Greece-UK
*--------------------------------*	

graph combine ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Greece greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Ireland greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Italy greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Netherlands greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Norway greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Portugal greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Spain greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi Sweden greyscale.gph" ///
	"analysis/graphs/combined/number of reasons accepted/graph components/bargraph of alg_govt_acceptable_scale_bi UK greyscale.gph" ///
	, ///
	title("Acceptance of algorithmic governance", size(medium)) ///
	subtitle("Number of reasons found acceptable by country", size(small)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
/*
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
*/
graph export "analysis/graphs/combined/number of reasons accepted/combined bargraphs of alg_govt_acceptable_scale_bi Greece-UK greyscale.png", width(2000) replace










*---------------------------------------*
* Rescale to 0-1
*---------------------------------------*


sum alg_govt_acceptable_scale_bi
replace alg_govt_acceptable_scale_bi = (alg_govt_acceptable_scale_bi -r(min)) / (r(max)-r(min))
sum alg_govt_acceptable_scale_bi
lab var alg_govt_acceptable_scale_bi "Binary acceptance of algorithmic governance (scaled 0-1)"

*---------------------------------------*
* Sub-scales
*---------------------------------------*

* Efficiency
egen alg_govt_acc_eff_scale_bi = rowtotal( alg_govt_acceptable_1_bi alg_govt_acceptable_2_bi alg_govt_acceptable_5_bi alg_govt_acceptable_7_bi alg_govt_acceptable_8_bi)

* Fairness
egen alg_govt_acc_fair_scale_bi = rowtotal( alg_govt_acceptable_3_bi alg_govt_acceptable_4_bi alg_govt_acceptable_6_bi) 

tab alg_govt_acc_fair_scale_bi alg_govt_acc_eff_scale_bi , cell

tabout ///
	alg_govt_acc_fair_scale_bi alg_govt_acc_eff_scale_bi ///
	using "analysis/tables/combined/Number of reasons of each subscale accepted.docx" ///
	, cells(cell) ///
	replace

count
local sample_count = r(N)

graph twoway ///
	(scatter alg_govt_acc_fair_scale_bi alg_govt_acc_eff_scale_bi , jitter(6) mcolor(ebblue%10) msize(tiny)) ///
	(lfit alg_govt_acc_fair_scale_bi alg_govt_acc_eff_scale_bi ) ///
	, ///
	title("Number of reasons accepted", size(medsmall)) ///
	subtitle("Comparing dimensions""Combined EPIS and Canadian data", size(small)) ///
	ytitle("Fairness") ///
	xtitle("Efficiency") ///
	ylabel(0(1)3) ///
	xlabel(0(1)5) ///
	legend(off) ///
	note("N = `sample_count'", size(vsmall))  ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
graph export "analysis/graphs/combined/scatterplots/scatterplot of alg_govt_acc_fair_scale_bi and alg_govt_acc_eff_scale_bi combined data.png", width(3000) replace


*------------------------------------------------------------------------------*
* Algorithmic governance acceptibility subscale categories
*------------------------------------------------------------------------------*

* Efficiency
tab alg_govt_acc_eff_scale_bi

gen alg_gov_sup_eff = 0
replace alg_gov_sup_eff = 1 if alg_govt_acc_eff_scale_bi >= 3

lab var alg_gov_sup_eff "Acceptance for a majority of efficiency reasons"
lab def alg_gov_sup_eff 0 "Does not accept majority" 1 "Accepts majority"
lab val alg_gov_sup_eff alg_gov_sup_eff

* Fairness
tab alg_govt_acc_fair_scale_bi

gen alg_gov_sup_fair = 0
replace alg_gov_sup_fair = 1 if alg_govt_acc_fair_scale_bi >= 2

lab var alg_gov_sup_fair "Support for a majority of fairness reasons"
lab def alg_gov_sup_fair 0 "Does not accept majority" 1 "Accepts majority"
lab val alg_gov_sup_fair alg_gov_sup_fair

* Twoway table
tab alg_gov_sup_eff alg_gov_sup_fair, cell

*------------------------------------------------------------------------------*
* Rescaling to 0-1
*------------------------------------------------------------------------------*

* Efficiency
sum alg_govt_acc_eff_scale_bi
replace alg_govt_acc_eff_scale_bi = (alg_govt_acc_eff_scale_bi -r(min)) / (r(max)-r(min))
lab var alg_govt_acc_eff_scale_bi "Binary acceptance of algorithmic governance efficiency items (scaled 0-1)"
sum alg_govt_acc_eff_scale_bi

* Fairness
sum alg_govt_acc_fair_scale_bi
replace alg_govt_acc_fair_scale_bi = (alg_govt_acc_fair_scale_bi -r(min)) / (r(max)-r(min))
sum alg_govt_acc_fair_scale_bi
lab var alg_govt_acc_fair_scale_bi "Binary acceptance of algorithmic governance fairness items (scaled 0-1)"

/* ALTERNATE WAY OF GRAPHING IT
preserve

collapse (count) N = gc, by(cps19_surveyday)

graph twoway (bar N cps19_surveyday , lcolor(ebblue*0.9) fcolor(eltblue%80)) ///
	, ///
	title("Acceptance of algorithmic governance", size(medium)) ///
	subtitle("CES 2019 Campaign Period Survey", size(small)) ///
	ytitle("Number of responses", size(small)) ///
	xtitle("Survey day", size(small)) ///
	xlab(1(2)39, valuelabel angle(45) labsize(small)) ///
	ylab(0(250)2000, angle(0) labsize(small)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)

restore
*/



********************************************************************************
* Creating anti-politics scale
********************************************************************************

*------------------------------------------------------------------------------*
* Populism
*------------------------------------------------------------------------------*

* Creating combined "populism" scale, and anti-elitism and strong leader subscales
* This is a bit awkward, as there are only 3 of the 7 usual items in the EPIS data

codebook populism_2 populism_4 populism_7

* Raw populism. Only the last item (populism_7) is reversed
gen POP2 = (populism_2 -1)/4
gen POP4 = (populism_4 -1)/4
gen POP7 = (populism_7*-1 +5)/4

sum POP2 POP4 POP7

egen populism_scale = rowmean(POP2 POP4 POP7)
lab var populism_scale "Populism (0-1)"

sum populism_scale

alpha POP2 POP4 POP7

factor POP2 POP4 POP7


* Anti-elitism subscale
egen antielitism = rowmean(POP2 POP7)
lab var antielitism "Anti-elitism (0-1)"

sum antielitism

sum POP2 POP7
alpha POP2 POP7
factor POP2 POP7


* Strong leader subscale
egen strongleader = rowmean(POP4)
lab var strongleader "Strong Leader (0-1)"

sum strongleader

sum POP4
*alpha POP4
*factor POP4

********************************************************************************
* Creating nativism scale
********************************************************************************
/*
codebook nativism_1 nativism_2 nativism_3 nativism_4 nativism_5 nativism_6

* Only the 4th Nativism item is reversed
gen NAT1 = (nativism_1 -1)/4
gen NAT2 = (nativism_2 -1)/4
gen NAT3 = (nativism_3 -1)/4
gen NAT4 = (nativism_4*-1 +5)/4
gen NAT5 = (nativism_5 -1)/4
gen NAT6 = (nativism_6 -1)/4

sum NAT1-NAT6

egen nativism_scale = rowmean(NAT1-NAT6)
lab var nativism_scale "Nativism (0-1)"

sum nativism_scale

alpha NAT1-NAT6
* Wow, that alpha is so much stronger

factor NAT1-NAT6
*/
********************************************************************************
* Rescaling other variables to 0-1
********************************************************************************

sum age_in_years
gen age_in_years_rescaled = (age_in_years -r(min)) / (r(max)-r(min))
sum age_in_years_rescaled
lab var age_in_years_rescaled "Age in years (rescaled 0-1)"

/* SWITCHED TO USING LOW INCOME FLAG
sum subjective_income
gen subjective_income_rescaled = (subjective_income -r(min)) / (r(max)-r(min))
sum subjective_income_rescaled
lab var subjective_income_rescaled "Subjective income (rescaled 0-1)"
*/


sum education_cat
gen education_cat_rescaled = (education_cat -r(min)) / (r(max)-r(min))
sum education_cat_rescaled
lab var education_cat_rescaled "Education category (rescaled 0-1)"

sum lr_scale
gen lr_scale_rescaled = (lr_scale -r(min)) / (r(max)-r(min))
sum lr_scale_rescaled
lab var lr_scale_rescaled "Left-Right scale (rescaled 0-1)"

/*

*standardizing the variable to 0-1
sum combined_jobloss_scale
replace combined_jobloss_scale = (combined_jobloss_scale-r(min))/ (r(max)-r(min))
sum combined_jobloss_scale

lab var combined_jobloss_scale "Personal and prosocial job loss expectations (-8/+8)"

*/


save "data/Combined EPIS and Canadian data recoded and labeled with scales.dta", replace



********************************************************************************
* Summarize resulting data
********************************************************************************

* Change the directory to the top one for this project
set more off
capture cd "C:\Users\balle\Dropbox (Personal)\Papers\Support for Algorithmic Governance"

log using "drafts/R&R files/Support for Algoroithmic Govenance R&R variable summary output.txt", text replace

use "data/Combined EPIS and Canadian data recoded and labeled with scales.dta", clear

foreach summary_variable of varlist ///
		alg_govt_acceptable_1_bi ///
		alg_govt_acceptable_2_bi ///
		alg_govt_acceptable_3_bi ///
		alg_govt_acceptable_4_bi ///
		alg_govt_acceptable_5_bi ///
		alg_govt_acceptable_6_bi ///
		alg_govt_acceptable_7_bi ///
		alg_govt_acceptable_8_bi ///
	alg_govt_acceptable_scale_bi ///
	age_in_years age_in_years_rescaled ///
	gender female ///
	low_income ///
	education_cat education_cat_rescaled ///
	lr_scale lr_scale_rescaled ///
	populism_scale ///
	country_recoded ///
///
 {
	noisily di "****************************************************"
	noisily di "Summary statistics of ""`summary_variable'"
	noisily di "****************************************************"
	noisily di ""
	
	noisily di "Table of ""`summary_variable'"
	tab `summary_variable', m
	noisily di ""
	
	noisily di "Summary of ""`summary_variable'"
	summarize `summary_variable', detail
	noisily di ""
	
	
	noisily di "Mode of ""`summary_variable'"
	modes `summary_variable'
	noisily di ""
	
 }

 log close



